<template>
	<div class='toc_IM_contacts'>
		
		<mt-search v-model="searchValue" placeholder="搜索人员">	
    		<div class='toc_IM_searchList'>
    			<div class='toc_list' v-for='item in search.resultList' v-if='item._id!=userId'>
    				<img class='avata' v-bind:src='item.avatar'>
    				<span class='main'>{{item.name}}</span>
    				<mt-button 
    					type="primary" 
    					size="small" 
    					style='float: right;' 
    					@click='openChat(item)' 
    					v-bind:disabled="allowCreate">
    					进入会话
    				</mt-button>
    			</div>
    		</div>
		</mt-search>
	</div>
</template>

<script>
	import systemConst from '@/assets/scripts/systemConst.js'
	import units from '@/assets/scripts/units.js'
	import TOC_IM_Mixin from '@/assets/scripts/TOC_IM_Mixin.js'
	import dataInterface from '@/assets/scripts/dataInterface.js'
	export default {
  	name: 'contacts',
  	data(){
    	return {
    		SystemConst:systemConst,
    		searchValue:'',
    		search:{
    			resultList:[],
    		},
    		allowCreate:false,
    		searchRundebounce:null
    	}
	},	
	watch:{
	  	searchValue(){
	  		this.searchRundebounce.setValue(this.searchValue);
	  		this.searchRundebounce.debounceftn();
	  	}
	},
	computed:{
  		userId(){return this.$store.state.userId},
  		messageList(){return this.$store.state.messageList.data},
  	},
  	mixins: [TOC_IM_Mixin],
  	mounted(){
  		this.headerController('联系人','contacts');
  		this.searchRundebounce = new units.Debounce({
			callback:data=>{
				this.searchRun(data)
			}
		});
  		this.searchRundebounce.initdebounce();
  	},
  	methods:{
  		searchRun(search){
  			dataInterface.searchAccountMem({name:search},res=>{
  				this.search.resultList=res;
  			})
		},
		checkMessageList(chatID){
	    	var result =false;
	    	var messageList =this.messageList;
	    	for(var a=0;a<messageList.length;a++){
	    		var item =messageList[a];
	    		if(chatID == item.chat._id){result =true;break;}
	    	};
	    	return result
	    },
		openChat(item){
			this.allowCreate=true;
			dataInterface.createChat({
				accountID:item.accountID,
    			chatType: this.SystemConst.ChatType.ChatSingle,
    			title: "TOC_IM_PRIVATE",
    			mem: [item._id,this.userId],
    			desc: "TOC_IM_PRIVATE",
    			approval: "1",
    			allowInvites: "1",
    			avatar: "TOC_IM_PRIVATE",
    			creator:this.userId,
    			createTime:new Date().getTime(),
    			updateTime:new Date().getTime()
			},(res)=>{
				this.allowCreate=false;
  				if(this.checkMessageList(res.chat._id)){
  					//已存在聊天列表
  				}else{
  					//新建在聊天列表写入记录
  					this.$store.commit({type:'topMessageList',newMsgItem:res});
  				};
  				this.$root.$router.push({path:'chat',query:{chatId:res.chat._id}});
			});
		}
  	}
  }
	
</script>

<style>
	div.toc_IM_contacts div.mint-searchbar{
		background: #f8f8f8;
		border-bottom: 1px solid #ededed;
	}
	div.toc_IM_searchList{
		width: 100%;
		display: table;
		box-sizing: border-box;
		min-height: 100px;
	}
	div.toc_IM_contacts div.mint-search-list{
		padding-top: 53px;
	}
</style>